Option Strict Off
Option Explicit On
Namespace BMLERP
	Friend Class frmORD130
		Inherits System.Windows.Forms.Form
		Dim OldPTPTC As String
		Private Sub WriteToFile()
			Dim SQLInsert As String
			If ISADDING Then
				SQLInsert = " INSERT INTO APT " & "(PTPTC,PTDSC,PTDAY,PTFDD,PTFMM,PTDCP) " & "VALUES " & "('" & txtPTPTC.Text & "', '" & txtPTDSC.Text & "'," & txtPTDAY.Text & "," & txtPTFDD.Text & "," & txtPTFMM.Text & "," & txtPTDCP.Text & ")"
			Else
				SQLInsert = "UPDATE APT SET PTPTC = '" & txtPTPTC.Text & "', PTDSC ='" & txtPTDSC.Text & "' " & ",PTDAY=" & txtPTDAY.Text & ",PTFDD=" & txtPTFDD.Text & ",PTFMM=" & txtPTFMM.Text & ",PTDCP=" & txtPTDCP.Text & " WHERE PTPTC ='" & OldPTPTC & "'"

			End If

			DBconn.Execute(SQLInsert)
		End Sub

		Private Sub WriteToScreen()
			Dim TBL As ADODB.Recordset
			If ISADDING Then 'gan rong
				txtPTPTC.Text = ""
				txtPTDSC.Text = ""
				txtPTDAY.Text = CStr(0)
				txtPTFDD.Text = CStr(0)
				txtPTFMM.Text = CStr(0)
				txtPTDCP.Text = CStr(0)
				' txtUMCVF = 0
			Else
				TBL = New ADODB.Recordset
				TBL.Open("  Select APT.*From APT where  PTPTC ='" & cboPTPTC.BoundText & "'", DBconn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic, ADODB.CommandTypeEnum.adCmdText)
				If TBL.EOF = False Then
					txtPTPTC.Text = IIf(IsDBNull(TBL.Fields("PTPTC").Value), "", TBL.Fields("PTPTC").Value)
					txtPTDSC.Text = IIf(IsDBNull(TBL.Fields("PTDSC").Value), "", TBL.Fields("PTDSC").Value)
					txtPTDAY.Text = IIf(IsDBNull(TBL.Fields("PTDAY").Value), 0, TBL.Fields("PTDAY").Value)
					txtPTFDD.Text = IIf(IsDBNull(TBL.Fields("PTFDD").Value), 0, TBL.Fields("PTFDD").Value)
					txtPTFMM.Text = IIf(IsDBNull(TBL.Fields("PTFMM").Value), 0, TBL.Fields("PTFMM").Value)
					txtPTDCP.Text = IIf(IsDBNull(TBL.Fields("PTDCP").Value), 0, TBL.Fields("PTDCP").Value)
				Else
					cboPTPTC.BoundText = ""
					txtPTPTC.Text = ""
					txtPTDSC.Text = ""
					txtPTDAY.Text = CStr(0)
					txtPTFDD.Text = CStr(0)
					txtPTFMM.Text = CStr(0)
					txtPTDCP.Text = CStr(0)
				End If
				TBL.Close()
			End If
			OldPTPTC = txtPTPTC.Text
		End Sub

		Sub ButtonStatus()
			cmdAddnew.Enabled = Not ISEDITING
			cmdEdit.Enabled = Not ISEDITING
			cmdSave.Enabled = ISEDITING	' And txtIMITN <> ""
			cmdDelete.Enabled = Not ISEDITING 'And txtIMITN <> ""
			cmdCancel.Enabled = ISEDITING
			Frame1.Enabled = ISEDITING
			'txtIMITN.Locked = Not ISADDING ' Lock item code tru truong hop ISADDING=true
			'txtDepartment.Enabled = ISEDITING
			'cboDepartment.Visible = Not ISADDING
			'txtDepartmentID.Visible = ISADDING
		End Sub

		Private Sub cboPTPTC_ClickEvent(ByVal eventSender As System.Object, ByVal eventArgs As AxMSDataListLib.DDataComboEvents_ClickEvent) Handles cboPTPTC.ClickEvent
			If eventArgs.area = 2 Then
				WriteToScreen()
			End If


		End Sub

		Private Sub cmdAddnew_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddnew.Click
			ISADDING = True
			ISEDITING = True
			WriteToScreen()

			' Ham Gan tri man hinh
			ButtonStatus()

		End Sub

		Private Sub cmdCancel_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCancel.Click
			' Gan tri man nhinh
			ISADDING = False
			ISEDITING = False

			ButtonStatus()
			txtPTPTC.Text = cboPTPTC.BoundText
			WriteToScreen()


		End Sub

		Private Sub cmdDelete_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDelete.Click
			Dim cmdExecute As Object
			Dim SQL As String
			SQL = "DELETE FROM APT WHERE PTPTC = '" & cboPTPTC.BoundText & "'"

			cmdExecute = New ADODB.Command
			cmdExecute.ActiveConnection = DBconn
			cmdExecute.CommandText = SQL
			cmdExecute.Execute()
			Adodc1.Refresh()
			'WriteToScreen (0)

		End Sub

		Private Sub cmdEdit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEdit.Click
			ISEDITING = True
			' Ham gan tri man hinh
			ISADDING = False
			ISEDITING = True


			ButtonStatus()

		End Sub

		Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
			Me.Close()

		End Sub

		Private Sub cmdSave_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSave.Click


			WriteToFile()
			Adodc1.Refresh()

			'''''''''''
			ISADDING = False
			ISEDITING = False

			ButtonStatus()


		End Sub

		Private Sub frmORD130_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
			'BIENCHUNG
			Adodc1.ConnectionString = ConnectString
			Adodc1.ConnectionTimeout = 10
			Adodc1.CommandType = ADODB.CommandTypeEnum.adCmdTable
			Adodc1.RecordSource = "APT"	'Term Of Payment

			Adodc1.Refresh()

			ISADDING = False
			ISEDITING = False
			ButtonStatus()
			VB6_AddADODataBinding()
			Me.cboPTPTC.RowSource = ADOBind_Adodc1.DataSource
		End Sub

		Private Sub txtPTPTC_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtPTPTC.KeyPress
			Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
			If KeyAscii = 13 Then
				If ValidMaso(OldPTPTC, txtPTPTC, "APT", " PTPTC='" & txtPTPTC.Text & "'") Then
					MsgBox("This code is already exist")
					txtPTPTC.Focus()
				Else
					txtPTDSC.Focus()
				End If
			End If
			eventArgs.KeyChar = Chr(KeyAscii)
			If KeyAscii = 0 Then
				eventArgs.Handled = True
			End If
		End Sub
	End Class
End Namespace